package com.gis.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gis.entity.User;
import com.gis.result.ResponseResult;
import com.gis.service.UserService;
import com.gis.utils.JWTUtils;
import com.gis.utils.PostgresqlUtil;

import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;



@RestController
@RequiredArgsConstructor
@Tag(name = "用户", description = "用户接口")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @Operation(summary = "获取所有用户")
    @GetMapping("/getUserAll")
    public ResponseResult<List<User>> GetUserAll(){
        List<User> userList = userService.findAllUser();
        return ResponseResult.success(userList);
    }

    @Operation(summary = "根据ID获取用户")
    @Parameters({
        @Parameter(name = "ID", description = "用户ID", required = true)
    })
    @GetMapping("/getUserByID")
    public ResponseResult<User> GetUserByID(@RequestParam Integer ID) {
        User u = userService.findByID(ID);
        return ResponseResult.success(u);
    }    

    @Operation(summary = "用户注册")
    @Parameters({
        @Parameter(name = "user", description = "用户", required = true)
    })
    @PostMapping("/register")
    public ResponseResult<String> Register(@RequestBody User user) {
        User u = userService.FindByUserName(user.getUsername());
        if(u != null) {
            return ResponseResult.fail("User already registered");
        }else {
            userService.Register(user.getUsername(), user.getPassword());
            return ResponseResult.success("Register Successfully");
        }
    }
    
    @Operation(summary = "用户登录")
    @Parameters({
        @Parameter(name = "user", description = "用户", required = true)
    })
    @PostMapping("/login")
    public ResponseResult<HashMap<String, Object>> Login(@RequestBody User user) {
        User u = userService.FindByUserName(user.getUsername());
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("user", user);
        String token = JWTUtils.GetToken(hashMap);
        hashMap.put("token", token);
        if(u != null && u.getPassword().equals(user.getPassword()))
        {

            return ResponseResult.success("Login in Successfully", hashMap);
        }
        return ResponseResult.fail("Login in failed");
    }
    
    @Operation(summary = "用户信息修改")
    @Parameters({
        @Parameter(name = "user", description = "用户", required = true)
    })
    @PostMapping("/update")
    public ResponseResult<String> UpdateUser(@RequestBody User user) {
        userService.UpdateUser(user);
        return ResponseResult.success("Update Successfully");
    }

}
